package com.liu.stockmarket

import android.os.Bundle
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.liu.stockmarket.databinding.ActivityMainBinding
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import com.liu.stockmarket.ui.datacenter.DataCenterActivity
import com.liu.stockmarket.ui.search.SearchActivity


class MainActivity : BaseActivity() {

    private lateinit var binding: ActivityMainBinding

    private val viewModel: MainViewModel by viewModels()


    override fun listener() {
        setToolbarButton("添加") {
            SearchActivity.start(this)
        }
    }

    override fun observer() {
        viewModel.isShowDataCenterBtn.observe(this){
            binding.fabAdd.visibility = if (it) View.VISIBLE else View.GONE
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setToolbar(binding.toolbar.toolbar)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_stock,
                R.id.navigation_fund,
                R.id.navigation_self,
                R.id.navigation_statistics, R.id.navigation_setting
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)


        // 设置按钮的距离底部位置，适配不同导航条高度
        binding.fabAdd.setOnClickListener {
            DataCenterActivity.start(this)
        }

//
        val layoutParams = binding.fabAdd.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.bottomMargin = layoutParams.bottomMargin + getNavigationBarHeight()
        binding.fabAdd.layoutParams = layoutParams
    }


}